<?php

namespace App\Models;

use Illuminate\Support\Facades\DB;


class AdminUsersModel
{

    const USERSDISABLE = 1;
    const USERSENABLE = 0;
    const SUPERUSERS = '*';


    public static function getAdminUsersList(array $params = array())
    {
        return self::getAdminUsersObj($params)->paginate(config('app.page_size'));
    }

    public static function getAllAdminUsers(array $params = array())
    {
        return self::getAdminUsersObj($params)->get();
    }

    public static function getAdminUsersExists(array $params = array())
    {
        return self::getAdminUsersObj($params)->exists();
    }

    public static function getAdminUsersData(array $params = array())
    {
        return self::getAdminUsersObj($params)->first();
    }

    public static function deleteAdminUsers(array $params = array())
    {
        return self::getAdminUsersObj($params)->delete();
    }

    public static function updateAdminUsers(array $params = array(), array $update = array())
    {
        return self::getAdminUsersObj($params)->update($update);
    }

    public static function getSalt()
    {
        return substr(sha1(time()), -10);
    }

    public static function insertAdminUsers(array $insert = array())
    {
        $time = time();
        if (!isset($insert['create_time'])) {
            $insert['create_time'] = $time;
        }
        if (!isset($insert['update_time'])) {
            $insert['update_time'] = $time;
        }
        return DB::table('admin_users')->insert($insert);
    }

    private static function getAdminUsersObj(array $params = array())
    {
        $adminUsers = DB::table('admin_users');
        if (isset($params['id']) && !empty($params['id'])) {
            if (is_array($params['id'])) {
                $adminUsers = $adminUsers->whereIn('id', $params['id']);
            } else {
                $adminUsers = $adminUsers->where('id', '=', $params['id']);
            }
        }
        if (isset($params['loginname']) && !empty($params['loginname'])) {
            $adminUsers = $adminUsers->where('loginname', '=', $params['loginname']);
        }
        if (isset($params['realname']) && !empty($params['realname'])) {
            $adminUsers = $adminUsers->where('realname', '=', $params['realname']);
        }
        if (isset($params['phone']) && !empty($params['phone'])) {
            $adminUsers = $adminUsers->where('phone', '=', $params['phone']);
        }
        if (isset($params['email']) && !empty($params['email'])) {
            $adminUsers = $adminUsers->where('email', '=', $params['email']);
        }
        if (isset($params['status']) && in_array($params['status'], [0, 1])) {
            $adminUsers = $adminUsers->where('status', '=', $params['status']);
        }
        return $adminUsers;
    }
}
